package com.dy.design_pattern_study.iterator_patern.dome1.impl;

import com.dy.design_pattern_study.iterator_patern.dome1.Iterator;

/**
 * @CreateBy zhangdingyi
 * @Date 2023-04-14
 * @Content 迭代器实现类
 */
public class ConcreteIterator extends Iterator {

    //定义一个具体的聚合对象
    private ConcreteAggregate aggregate;
    private int current = 0;

    //初始化的时候传入一个聚合对象
    public ConcreteIterator(ConcreteAggregate aggregate) {
        this.aggregate = aggregate;
    }

    @Override
    public Object first() {
        //得到一个聚合对象
        return aggregate.getItem(0);
    }

    /**
     * 获取下一个数据
     *
     * @return
     */
    @Override
    public Object next() {
        Object ret = null;
        current++;
        if (current < aggregate.count()) {
            ret = aggregate.getItem(current);
        }
        return ret;
    }

    @Override
    public boolean isDone() {
        //判断当前是否遍历到结尾
        return current >= aggregate.count() ? true : false;
    }

    @Override
    public Object currentItem() {
        //返回当前聚合对象
        return aggregate.getItem(current);
    }
}
